/**
 * ztree插件的封装
 */
(function() {

    var CommonZTree = function(id, url) {
        this.id = id;
        this.url = url;
        this.onClick = function (e, treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("treeContainer");
            zTree.checkNode(treeNode, !treeNode.checked, null, true);
            return false;
        };
        this.settings = null;
        this.ondblclick=null;
    };

    CommonZTree.prototype = {
        /**
         * 初始化ztree的设置
         */
        initSetting : function() {
            var settings = {
                view : {
                    dblClickExpand : true,
                    selectedMulti : false
                },
                data : {simpleData : {enable : true}},
                callback : {
                    onClick : this.onClick,
                    onDblClick:this.ondblclick
                }
            };
            return settings;
        },

        /**
         * 手动设置ztree的设置
         */
        setSettings : function(val) {
            this.settings = val;
        },

        /**
         * 初始化ztree
         */
        init : function() {
            var zNodeSeting = null;
            if(this.settings != null){
                zNodeSeting = this.settings;
            }else{
                zNodeSeting = this.initSetting();
            }
            var zNodes = this.loadNodes();
            $.fn.zTree.init($("#" + this.id), zNodeSeting, zNodes);
        },

        /**
         * 绑定onclick事件
         */
        bindOnClick : function(func) {
            this.onClick = func;
        },
        /**
         * 绑定双击事件
         */
        bindOnDblClick : function(func) {
            this.ondblclick=func;
        },


        /**
         * 加载节点
         */
        loadNodes : function() {
            var zNodes;
            $.ajax({
                url: this.url,
                async: false,
                success: function (res) {
                    zNodes = res;
                },
                fail: function (res) {
                    console.log("fail");
                    console.log(res);
                }
            });
            return zNodes;
        },

        /**
         * 获取选中的值
         */
        getSelectedVal : function(){
            var zTree = $.fn.zTree.getZTreeObj(this.id);
            var nodes = zTree.getSelectedNodes();
            return nodes[0].name;
        }
    };

    window.CommonZTree = CommonZTree;

}());